package com.ai.util;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 * 煤矿短信发送工具类
 * @Author jiaok
 * @Date 2024-11-20 14:13
 */
@Slf4j
public class CoalTextMessageUtil {

    final static String serverurl = "http://11.1.210.89:8080/XJSMS/web2/phoneserver2";

    public static String  Send(String phone, String content){
        // 表单参数与get形式一样
        String retstring = "";
        StringBuffer params = new StringBuffer();
        params.append("content").append("=").append(content).append("&").append("phonenumber").append("=").append(phone);
        String post = "";
        post = params.toString();

        try {
            URL url = new URL(serverurl);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            // 提交模式
            httpURLConnection.setRequestMethod("POST");
            // conn.setConnectTimeout(10000);//连接超时 单位毫秒
            // conn.setReadTimeout(2000);//读取超时 单位毫秒
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
            // 发送请求参数  post的参数 xx=xx&yy=yy
            printWriter.write(post);
            // flush输出流的缓冲
            printWriter.flush();
            //开始获取数据
            BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int len;
            byte[] arr = new byte[1024];
            while((len=bis.read(arr))!= -1){
                bos.write(arr,0,len);
                bos.flush();
                String str = new String(arr);
                retstring = retstring + str;
            }
            bos.close();
        } catch (Exception e) {
            log.error("exception is " + e.getMessage());
        }
        return "";
    }

    public static String SendMessage(String phone, String content) {
        // 表单参数
        StringBuffer params = new StringBuffer();
        params.append("content").append("=").append(content).append("&").append("phonenumber").append("=").append(phone);

        String post = params.toString();
        // 用于存储响应信息
        String retstring = "";

        try {
            // 创建 URL 和连接对象
            URL url = new URL(serverurl);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

            // 设置请求方法和超时
            httpURLConnection.setRequestMethod("POST");
            // 连接超时：10秒
            httpURLConnection.setConnectTimeout(10000);
            // 读取超时：5秒
            httpURLConnection.setReadTimeout(5000);

            // 允许输入输出
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);

            // 写入请求参数
            try (PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream())) {
                printWriter.write(post);
                printWriter.flush();
            }

            // 检查响应码
            int responseCode = httpURLConnection.getResponseCode();
            log.info("Response Code: " + responseCode);

            // 读取响应数据
            try (BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
                 ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
                byte[] buffer = new byte[1024];
                int len;
                while ((len = bis.read(buffer)) != -1) {
                    bos.write(buffer, 0, len);
                }
                // 解析为 UTF-8 编码的字符串
                retstring = bos.toString("UTF-8");
            }

            // 打印响应内容
           log.info("===========================  Response : {} ===========================",retstring);

        } catch (Exception e) {
            log.error("===========================   短信发送异常   ===========================");
            e.printStackTrace();
        }

        // 返回服务器响应内容
        return retstring;
    }


}
